package br.com.estevam.sisfin.components;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.IncludeJavaScriptLibrary;
import org.apache.tapestry5.annotations.IncludeStylesheet;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.PageRenderLinkSource;

import br.com.estevam.sisfin.pages.Portal;
import br.com.estevam.sisfin.tools.MenuItem;

@IncludeJavaScriptLibrary("context:menu/menu.js")
@IncludeStylesheet("context:menu/menu.css")
public class Menu {

	@Parameter(required = true)
	private MenuItem model;

	@Inject
	private ComponentResources componentResources;

	@Inject
	private PageRenderLinkSource linkSource;

	private String parentName;

	public boolean beginRender(MarkupWriter writer) {
//		System.out.println(componentResources.getContainerResources().getCompleteId());
		parentName = componentResources.getContainerResources().getCompleteId().replace(':', '.').toLowerCase();

		if(parentName.lastIndexOf('/') != 0){
			parentName = parentName.substring(parentName.lastIndexOf('/') + 1,parentName.length());
		}

		writer.element("div", "id", "menu");
		writer.element("ul", "class","level1", "id","root");
		drawMenu(model, writer);
		writer.end();
		writer.end();

		return true;
	}

	private void drawMenu(MenuItem model, MarkupWriter writer){
		for(MenuItem item : model.getSubItens()){
			writer.element("li");
			if(item.getSubItens().size() == 0){
				if(item.isPagelink()){
					item.setId(item.getId().replace("-", "/"));
					// writer.element("type:pagelink", "page", item.getId());
					// writer.element("a", "href", "#", "t:type", "pagelink", "page", item.getId());
					// writer.element("a", "href", item.getId());	
					writer.element("a", "href", linkSource.createPageRenderLink(item.getId()));
					writer.write(item.getName());
					writer.end();
				} else {
					// writer.element("a", "href", parentName + ".menu.redirect/" + item.getId());
					writer.element("a", "href", linkSource.createPageRenderLink(Portal.class) + ".layout.menubuilder.menu.redirect/" + item.getId());			
					writer.write(item.getName());
					writer.end();
				}
			} else {
				writer.element("a","href","#");
				writer.write(item.getName());
				writer.end();

				writer.element("ul","class","level2");
				drawMenuSub(item, writer);
				writer.end();
			}
			writer.end();

			writer.element("li", "class","sep");
			writer.write("|");
			writer.end();
		}
	}

	private void drawMenuSub(MenuItem model, MarkupWriter writer){
		for(MenuItem item : model.getSubItens()){
			writer.element("li");
			if(item.getSubItens().size() == 0){
				if(item.isPagelink()){
					item.setId(item.getId().replace("-", "/"));
					// writer.element("type:pagelink", "page", item.getId());
					// writer.element("a", "href", "#", "t:type", "pagelink", "page", item.getId());
					// writer.element("a", "href", item.getId());	
					writer.element("a", "href", linkSource.createPageRenderLink(item.getId()));
					writer.write(item.getName());
					writer.end();
				} else {
					// writer.element("a", "href", parentName + ".menu.redirect/" + item.getId());
					writer.element("a", "href", linkSource.createPageRenderLink(Portal.class) + ".layout.menubuilder.menu.redirect/" + item.getId());
					writer.write(item.getName());
					writer.end();
				}
			} else {
				writer.element("a","href","#");
				writer.write(item.getName());
				writer.end();

				writer.element("ul","class","level3");
				drawMenuSub(item, writer);
				writer.end();
			}
			writer.end();
		}
	}

	public void onActionFromRedirect(String s){
		componentResources.getContainerResources().triggerEvent("onActionFromMenu", new Object[]{s}, null);
	}

	public MenuItem getModel() {
		return model;
	}

	public void setModel(MenuItem model) {
		this.model = model;
	}

}
